package 基础.reflect反射_注解;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定的构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p1 = new Person();
        System.out.println(p1);

        Person p2 = new Person("李四",30);
        System.out.println(p2);

        //1获取类对象
        Class cls = Class.forName("tedu.api.reflect.Person");
        /*
            2
            java.lang.reflect.Constructor
            Constructor的每一个实例用于表示一个构造器,使用它可以相当于调用该构造器实例化对象
         */
//        Constructor c =  cls.getConstructor();//获取默认无参的构造器
//        c.newInstance();//不需要传入实参
        Constructor c = cls.getConstructor(String.class,int.class);
        Object obj = c.newInstance("wangwu",60);//new Person("王五",60);
        System.out.println(obj);
    }
}
